package com.gxa.pingan.service.impl;

import com.gxa.pingan.common.ExceptionCode;
import com.gxa.pingan.exception.BusinessException;
import com.gxa.pingan.mapper.DriverMapper;
import com.gxa.pingan.pojo.Driver;
import com.gxa.pingan.service.DriverService;
import com.gxa.pingan.utils.MD5Util;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author ChenJin
 * @date 2021/10/28 12:48
 * Description:
 **/
@Service(value = "DriverService")
@Transactional(rollbackFor = {Throwable.class})
public class DriverServiceImpl implements DriverService {

    @Resource
    private DriverMapper driverMapper = null;

    /**
     * 添加司机
     *
     * @param driver
     */
    @Override
    public void insert(Map<String,Object> driver) throws BusinessException {
        Driver driverByPhone = driverMapper.selectByPhone(driver.get("phone").toString());
        if (driverByPhone != null) {
            throw new BusinessException(ExceptionCode.HAS_USER);
        }
        Driver driverByIdCard = driverMapper.selectByIdCard(driver.get("idCard").toString());
        if (driverByIdCard != null) {
            throw new BusinessException(ExceptionCode.HAS_ID_CARD);
        }
        List<Driver> list = driverMapper.getList(null, null, null);
        driver.put("id",Long.valueOf(list.size() + 1));
        driver.put("passWord",MD5Util.encode((driver.get("passWord").toString()) ));
        if (Integer.valueOf(driver.get("status").toString()) == 1) {
            driver.put("status",true);
        }else {
            driver.put("status",false);
        }
        driverMapper.insert(driver);
    }

    /**
     * 获取司机信息
     *
     * @param id 司机编号
     * @return
     * @throws BusinessException
     */
    @Override
    public Driver getDriver(Long id) throws BusinessException {
        Driver driver = driverMapper.selectById(id);
        if (driver == null) {
            throw new BusinessException(ExceptionCode.NO_USER);
        }
        return driver;
    }

    /**
     * 获取司机列表信息
     *
     * @param value 搜索框的值
     * @param page  页码
     * @param size  大小
     * @return
     * @throws BusinessException
     */
    @Override
    public List<Driver> getList(String value, Integer page, Integer size) throws BusinessException {
        List<Driver> list = driverMapper.getList(value, (page - 1) * size, size);
        if (list.size() == 0) {
            throw new BusinessException(ExceptionCode.NO_DATA);
        }
        return list;
    }

    /**
     * 获取司机列表数据总数
     *
     * @param value
     * @return
     * @throws BusinessException
     */
    @Override
    public Integer getSize(String value) throws BusinessException {
        List<Driver> list = driverMapper.getList(value, null, null);
        if (list.size() == 0) {
            throw new BusinessException(ExceptionCode.NO_DATA);
        }
        return list.size();
    }

    /**
     * 修改司机信息
     *
     * @param driver 司机对象
     * @throws BusinessException
     */
    @Override
    public void update(Map<String, Object> driver) throws BusinessException {
        System.out.println(driver);
        Driver updateDriver = driverMapper.selectById(Long.valueOf(driver.get("id").toString()));
        if (updateDriver == null) {
            throw new BusinessException(ExceptionCode.FAILED_OPERATION);
        }
        if (Integer.valueOf(driver.get("status").toString()) == 1) {
            driver.put("status",true);
        }else {
            driver.put("status",false);
        }
        driverMapper.update(driver);
    }

    /**
     * 导出司机信息
     *
     * @return
     * @throws BusinessException
     */
    @Override
    public List<Driver> upload() throws BusinessException {
        List<Driver> list = driverMapper.getList(null, null, null);
        if (list.size() == 0) {
            throw new BusinessException(ExceptionCode.NO_DATA);
        }
        return list;
    }
}
